import { PaginationQueryDto } from './../../common/dto/paginationQuery.dto';
import { RoleService } from './role.service';
import { SortMenuDto } from './dto/sort.dto';
import { UpdateMenuDto } from './dto/updateMenu.dto';
import { StaffService } from './staff.service';
import { LoginDto } from '../user/dto/login.dto';
import { Body, Controller, Post, BadRequestException, Res } from '@nestjs/common';
import { NotAuthorized } from 'src/decorator/roles.decorator';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/createMenu.dto';
import { ApiOperation } from '@nestjs/swagger';
import { DictService } from './dict.service';
import { CreateDictDataDto, CreateDictDto } from './dto/createDict.dto';


@Controller('admin')
export class AdminController {
    constructor(
        private readonly staffServ: StaffService,
        private readonly menuServ: MenuService,
        private readonly roleServ: RoleService,
        private readonly dictServ:DictService
    ) { }

    @NotAuthorized()
    @Post('login')
    login(@Body() dto: LoginDto) {
        return this.staffServ.login(dto);
    }
    
    /**
     *  字典管理部分
     */
    @NotAuthorized() 
    @Post('dicts')
    getDicts(@Body() dto:PaginationQueryDto) {
        return this.dictServ.queryDicts(dto);
    }

    @ApiOperation({summary:"添加字典"})
    @Post('addDict')
    addDict(@Body() dto:CreateDictDto) {
       return this.dictServ.addDict(dto); 
    }

    @ApiOperation({summary:"添加字典项数据"})
    @Post('addDictData')
    addDictData(@Body() dto:CreateDictDataDto) {
       return this.dictServ.addDictData(dto); 
    }

    @ApiOperation({summary:"删除字典项数据"})
    @Post('removeDict')
    removeDict(@Body('id') id:string) {
        if(!id) {
            throw new BadRequestException('请传入id');
        }
        return this.dictServ.removeDict(id);
    }

    /**
     * 角色部分接口
     */
    @Post('roles')
    getRoles(@Body() dto: PaginationQueryDto = { pageSize: 10, current: 1 }) {
        return this.roleServ.pagination(dto);
    }

    @Post('menus/role')
    getRoleMenu(@Body() body: { id: string }) {
        if(!body.id) {
            throw new BadRequestException('请传入id');
        }
        return this.roleServ.queryRoleMenuId(body.id);
    }

    /**   
     *  菜单部分接口
     */
    @Post('menus')
    getMenus() {
        return this.menuServ.queryMenus();
    }

    @Post('addMenu')
    addMenu(@Body() dto: CreateMenuDto) {
        return this.menuServ.addMenu(dto);
    }

    @Post('update')
    updateMenu(@Body() dto: UpdateMenuDto) {
        if (dto.pId === '') delete dto.pId;
        return this.menuServ.updateById(dto.id, dto);
    }

    @Post('delete')
    deleteMenu(@Body() dto: UpdateMenuDto) {
        return this.menuServ.deleteById(dto.id);
    }

    @ApiOperation({ summary: '排序上移' })
    @Post('sort')
    sort(@Body() dto: SortMenuDto) {

        return this.menuServ.order(dto);
    }

}
